package com.shell.governor.modules.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.governor.modules.po.PubBizEnt;
import com.shell.governor.modules.po.PubBizRes;
import com.shell.governor.modules.po.PubBizRole;
import com.shell.governor.modules.po.PubBusiness;
import com.shell.governor.modules.service.PubBizEntService;
import com.shell.governor.modules.service.PubBizResService;
import com.shell.governor.modules.service.PubBizRoleService;
import com.shell.governor.modules.service.PubBusinessService;

@Service
public class PubBusinessServiceImpl extends PersistentServiceImpl<PubBusiness> implements PubBusinessService {
	
	@Autowired
	PubBizResService pubBizResService;
	@Autowired
	PubBizRoleService pubBizRoleService;
	@Autowired
	PubBizEntService pubBizEntService;

	public void delBiz(String bizId) {
		delete(bizId);

		PubBizRes bizRes = new PubBizRes();
		bizRes.setBizId(bizId);
		this.pubBizResService.deleteEntity(bizRes);

		PubBizRole bizRole = new PubBizRole();
		bizRole.setBizId(bizId);
		this.pubBizRoleService.deleteEntity(bizRole);
	}

	public void openBizByCodes(String entId, String codes) {
		if ((StringUtil.empty(codes)) || (StringUtil.empty(entId))) {
			return;
		}
		String[] bizCodes = codes.split(",");
		for (String bizCode : bizCodes) {
			PubBusiness biz = new PubBusiness();
			biz.setBizCode(bizCode);
			biz = (PubBusiness) selectOne(biz);
			if (biz != null) {
				PubBizEnt bizEnt = new PubBizEnt();
				bizEnt.setBizId(biz.getBizId());
				bizEnt.setEntId(entId);
				bizEnt = (PubBizEnt) this.pubBizEntService.selectOne(bizEnt);
				if (bizEnt == null) {
					bizEnt = new PubBizEnt();
					bizEnt.setBizId(biz.getBizId());
					bizEnt.setEntId(entId);
					this.pubBizEntService.save(bizEnt);
				}
			}
		}
	}

	public void openBizByBizIds(String entId, String bizIds) {
		if ((StringUtil.empty(bizIds)) || (StringUtil.empty(entId))) {
			return;
		}
		String[] idsArr = bizIds.split(",");
		for (String bizId : idsArr) {
			PubBizEnt bizEnt = new PubBizEnt();
			bizEnt = new PubBizEnt();
			bizEnt.setBizId(bizId);
			bizEnt.setEntId(entId);
			this.pubBizEntService.save(bizEnt);
		}
	}
}
